package com.module.user.ui.info

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.common.app.data.bean.UpFileModel
import com.common.app.data.bean.user.UserBean
import com.common.app.data.repository.UserApi
import com.common.base.app.viewmodel.BaseViewModel
import com.common.base.data.CommonBean
import com.common.base.utils.AppUtils.getString
import com.common.base.utils.CacheManager
import com.module.user.R
import java.io.File

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/8/29
 * <p>
 * 描述：个人资料VM
 * <p>
 * 修订历史：
 */
class PersonInfoViewModel : BaseViewModel() {

    val cacheLD = MutableLiveData<String>()

    fun getCache(context: Context) {
        cacheLD.value = CacheManager.getTotalCacheSize(context)
    }

    fun clearCache(context: Context) {
        CacheManager.clearAllCache(context)
        getCache(context)
    }

    //获取用户信息
    fun getUserInfo(): LiveData<CommonBean<UserBean>> = emit({}) {
        UserApi.getUserInfo()
    }

    fun uploadImage(url: File): LiveData<CommonBean<UpFileModel>> =
        emit({ showErrorToast(it.message ?: getString(R.string.common_upload_failed_hint)) }) {
            UserApi.uploadHeadImage(url)
        }


    fun uploadImage(url: File, type:String): LiveData<CommonBean<UpFileModel>> =
        emit({ showErrorToast(it.message ?: getString(R.string.common_upload_failed_hint)) }) {
            UserApi.uploadHeadImage(url,type)
        }


    //修改用户数据
    fun modifyUsrInfo(imageId: Long = 0, nickname: String?=null, birthday: String?=null, height: Int = 0, weight: Int = 0, sex: Int = 1,description: String?=null,): LiveData<CommonBean<Any>> = emit({}) {
        UserApi.modifyUsrInfo(imageId,nickname,birthday,height,weight,sex,description)
    }
}